<?php

/**
 * BonSortieExterne form base class.
 *
 * @method BonSortieExterne getObject() Returns the current form's model object
 *
 * @package    symfony
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormGeneratedTemplate.php 29553 2010-05-20 14:33:00Z Kris.Wallsmith $
 */
abstract class BaseBonSortieExterneForm extends BaseFormDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                    => new sfWidgetFormInputHidden(),
      'reference'             => new sfWidgetFormInputText(),
      'date_creation'         => new sfWidgetFormDate(),
      'date_validite'         => new sfWidgetFormDate(),
      'client_id'             => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Client'), 'add_empty' => true)),
      'departement_source_id' => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('DepartementSource'), 'add_empty' => true)),
      'representant_src_id'   => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Representant'), 'add_empty' => true)),
      'observation'           => new sfWidgetFormTextarea(),
      'statut'                => new sfWidgetFormChoice(array('choices' => array('Affectation de Stock' => 'Affectation de Stock', 'Ajustement de Stock' => 'Ajustement de Stock'))),
      'confirme'              => new sfWidgetFormInputCheckbox(),
      'valider'               => new sfWidgetFormInputCheckbox(),
      'etat_reglement'        => new sfWidgetFormInputCheckbox(),
      'supprimer'             => new sfWidgetFormInputCheckbox(),
      'livre'                 => new sfWidgetFormInputCheckbox(),
      'cloture'               => new sfWidgetFormInputCheckbox(),
      'created_at'            => new sfWidgetFormDateTime(),
      'updated_at'            => new sfWidgetFormDateTime(),
      'm_article_list'        => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Article')),
    ));

    $this->setValidators(array(
      'id'                    => new sfValidatorChoice(array('choices' => array($this->getObject()->get('id')), 'empty_value' => $this->getObject()->get('id'), 'required' => false)),
      'reference'             => new sfValidatorString(array('max_length' => 20)),
      'date_creation'         => new sfValidatorDate(),
      'date_validite'         => new sfValidatorDate(array('required' => false)),
      'client_id'             => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Client'), 'required' => false)),
      'departement_source_id' => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('DepartementSource'), 'required' => false)),
      'representant_src_id'   => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Representant'), 'required' => false)),
      'observation'           => new sfValidatorString(array('max_length' => 1000, 'required' => false)),
      'statut'                => new sfValidatorChoice(array('choices' => array(0 => 'Affectation de Stock', 1 => 'Ajustement de Stock'), 'required' => false)),
      'confirme'              => new sfValidatorBoolean(array('required' => false)),
      'valider'               => new sfValidatorBoolean(array('required' => false)),
      'etat_reglement'        => new sfValidatorBoolean(array('required' => false)),
      'supprimer'             => new sfValidatorBoolean(),
      'livre'                 => new sfValidatorBoolean(),
      'cloture'               => new sfValidatorBoolean(array('required' => false)),
      'created_at'            => new sfValidatorDateTime(),
      'updated_at'            => new sfValidatorDateTime(),
      'm_article_list'        => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Article', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('bon_sortie_externe[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    $this->setupInheritance();

    parent::setup();
  }

  public function getModelName()
  {
    return 'BonSortieExterne';
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['m_article_list']))
    {
      $this->setDefault('m_article_list', $this->object->MArticle->getPrimaryKeys());
    }

  }

  protected function doSave($con = null)
  {
    $this->saveMArticleList($con);

    parent::doSave($con);
  }

  public function saveMArticleList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['m_article_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->MArticle->getPrimaryKeys();
    $values = $this->getValue('m_article_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('MArticle', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('MArticle', array_values($link));
    }
  }

}
